package erp

import (
	"context"
	"encoding/json"
	"ez/apps/erp/auto/mc"
	"gitee.com/dreamwood/ez-go/db/mgo"
	"gitee.com/dreamwood/ez-go/ez"
	"gitee.com/dreamwood/ez-go/ss"
	"time"
)

type Need struct {
	mgo.BaseDoc   `bson:"-" json:"-"`
	Id            int64          `json:"id" bson:"id,omitempty"`
	Sn            string         `json:"sn" bson:"sn"`                       //单据编号
	Title         string         `json:"title" bson:"title"`                 //单据主题
	Status        *NeedStatus    `json:"status" bson:"status"`               //状态
	StatusId      int64          `json:"statusId" bson:"statusId"`           //状态
	IsClose       bool           `json:"isClose" bson:"isClose"`             //是否关闭
	FormAt        time.Time      `json:"formAt" bson:"formAt"`               //制单时间
	NeedAt        time.Time      `json:"needAt" bson:"needAt"`               //需求时间
	FormEmp       *Emp           `json:"formEmp" bson:"formEmp"`             //制单人
	FormEmpId     int64          `json:"formEmpId" bson:"formEmpId"`         //制单人
	CreateEmp     *Emp           `json:"createEmp" bson:"createEmp"`         //创建人
	CreateEmpId   int64          `json:"createEmpId" bson:"createEmpId"`     //创建人
	NeedEmp       *Emp           `json:"needEmp" bson:"needEmp"`             //需求人
	NeedEmpId     int64          `json:"needEmpId" bson:"needEmpId"`         //需求人
	NeedDept      *Department    `json:"needDept" bson:"needDept"`           //需求部门
	NeedDeptId    int64          `json:"needDeptId" bson:"needDeptId"`       //需求部门
	Note          string         `json:"note" bson:"note"`                   //备注
	Product       []*NeedProduct `json:"product" bson:"product"`             //产品
	Flow          int64          `json:"flow" bson:"flow"`                   //审批流程
	FlowIndex     int64          `json:"flowIndex" bson:"flowIndex"`         //当前审批节点
	FlowName      string         `json:"flowName" bson:"flowName"`           //当前审批节点
	FlowCur       ss.M           `json:"flowCur" bson:"flowCur"`             //当前审批节点
	Flows         []ss.M         `json:"flows" bson:"flows"`                 //审批过程
	IsConfirm     bool           `json:"isConfirm" bson:"isConfirm"`         //是否生效
	ConfirmAt     time.Time      `json:"confirmAt" bson:"confirmAt"`         //生效时间
	IsCancel      bool           `json:"isCancel" bson:"isCancel"`           //是否取消
	IsLock        bool           `json:"isLock" bson:"isLock"`               //是否锁定
	ApprovalSate  string         `json:"approvalSate" bson:"approvalSate"`   //评审状态
	ApprovalStage string         `json:"approvalStage" bson:"approvalStage"` //评审环节
	CreateAt      time.Time      `json:"createAt" bson:"createAt"`
	UpdateAt      time.Time      `json:"updateAt" bson:"updateAt"`
	DeleteAt      *time.Time     `json:"deleteAt" bson:"deleteAt"`
}

func (this *Need) DocName() string { return "Need" }
func (this *Need) GetId() int64    { return this.Id }
func (this *Need) SetId(id int64)  { this.Id = id }
func (this *Need) Create() error {
	return this.GetFactory().Create(this)
}
func (this *Need) Replace() error {
	return this.GetFactory().Replace(this)
}
func (this *Need) Save() error {
	if this.Id == 0 {
		this.CreateAt = time.Now()
		this.UpdateAt = time.Now()
		return this.GetFactory().Create(this)
	} else {
		this.UpdateAt = time.Now()
		return this.GetFactory().Replace(this)
	}
}

// 伪删除
func (this *Need) Delete() error {
	return this.GetFactory().Delete(this)
}
func (this *Need) UnDelete() error {
	return this.GetFactory().UnDelete(this)
}

// 真删除
func (this *Need) Destroy() error {
	return this.GetFactory().Destroy(this)
}
func (this *Need) ToString() string {
	return string(this.ToBytes())
}
func (this *Need) ToBytes() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *Need) Serialize() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *Need) UnSerialize(data []byte) []byte {
	ez.Try(json.Unmarshal(data, this))
	return data
}
func (this *Need) LoadStatus() {
	if this.StatusId == 0 {
		return
	}
	this.Status, _ = NewNeedStatusCrud().FindId(this.StatusId)
}
func (this *Need) LoadFormEmp() {
	if this.FormEmpId == 0 {
		return
	}
	this.FormEmp, _ = NewEmpCrud().FindId(this.FormEmpId)
}
func (this *Need) LoadCreateEmp() {
	if this.CreateEmpId == 0 {
		return
	}
	this.CreateEmp, _ = NewEmpCrud().FindId(this.CreateEmpId)
}
func (this *Need) LoadNeedEmp() {
	if this.NeedEmpId == 0 {
		return
	}
	this.NeedEmp, _ = NewEmpCrud().FindId(this.NeedEmpId)
}
func (this *Need) LoadNeedDept() {
	if this.NeedDeptId == 0 {
		return
	}
	this.NeedDept, _ = NewDepartmentCrud().FindId(this.NeedDeptId)
}
func (this *Need) LoadProduct() {
	product, _ := NewNeedProductCrud().FindBy(ss.M{"belongToId": this.Id}, []string{"id"}, 0, 0)
	this.Product = product
}
func (this *Need) ClearRelationsBeforeSave() mgo.Doc {
	this.Status = nil
	this.FormEmp = nil
	this.CreateEmp = nil
	this.NeedEmp = nil
	this.NeedDept = nil
	this.Product = nil
	return this
}
func neverUsed_Need() {
	//导入ss包
	a := ss.M{}
	ez.Debug(a)
}

type NeedAccessControl struct {
	Access  bool
	Message string
	Action  string //控制器Action,小写开头，如c,u,r,d
	Model   *Need
	Session *ez.Session
}

func NewNeedAccessControl(model *Need, action string, session *ez.Session) *NeedAccessControl {
	ctrl := &NeedAccessControl{
		Access:  true,
		Model:   model,
		Action:  action,
		Session: session,
	}
	ez.DispatchToMany(mc.NeedAccessControlEvent, ctrl, context.TODO())
	return ctrl
}
